package commands.gameCommand
{
	import Iterator.IIterator;
	
	import flash.display.Sprite;
	import flash.events.Event;
	
	public class Gameboard extends Sprite
	{
		private var _data:GameboardData;
		private var _newGamePiece:Piece;
		private var _pieces:Sprite;
		public function Gameboard()
		{
			
		}

		public function get data():GameboardData
		{
			return _data;
		}

		public function set data(value:GameboardData):void
		{
			_data = value;
			onUpdate();
			_data.addEventListener(Event.CHANGE,onUpdate);
		}
		private function onUpdate(event:Event=null):void{
			_pieces = new Sprite;
			addChild(_pieces);
			var iterator:IIterator = _data.iterator();
			var piece:Piece;
			while(iterator.hasNext()){
				piece = new Piece();
				var pd:Object = iterator.next();
				//if(pd){
					piece.data = pd as PieceData;
					_pieces.addChild(piece);
				//}
			}
			if(_newGamePiece == null){
				_newGamePiece = new Piece;
				_newGamePiece.data = _data.newGamePiece;
				_newGamePiece.data.radius = 40;
				addChild(_newGamePiece);
			}
			_newGamePiece.x = _newGamePiece.width/2;
			_newGamePiece.y = _newGamePiece.height + _newGamePiece.y + 40;
		}

	}
}